/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tela;

import java.util.List;
import lexico.AnalisadorLexico;
import lexico.TipoToken;
import lexico.Token;

/**
 *
 * @author Administrador
 */
public class JFPrincipal extends javax.swing.JFrame {
    
    private List<Token> tokens;

    
    public JFPrincipal() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTCodigoFonte = new javax.swing.JTextArea();
        jBRun = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        textoTabela = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Analisador Léxico - MiniJava");

        jTCodigoFonte.setColumns(20);
        jTCodigoFonte.setRows(5);
        jScrollPane1.setViewportView(jTCodigoFonte);

        jBRun.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tela/run.png"))); // NOI18N
        jBRun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRunActionPerformed(evt);
            }
        });

        textoTabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Lexema", "Classe", "Posição"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        textoTabela.getTableHeader().setReorderingAllowed(false);
        jScrollPane3.setViewportView(textoTabela);

        jLabel1.setText("Código Fonte:");

        jLabel2.setText("Tabela de Tokens:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(491, 491, 491)
                        .addComponent(jBRun, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 593, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jBRun)
                .addGap(81, 81, 81)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRunActionPerformed
      
        AnalisadorLexico analisador = new AnalisadorLexico();
        analisador.setCodigoFonte(jTCodigoFonte.getText());//pega tudo o que foi escrito no formulario
        analisador.analisar();// realiza toda a analise lexica        
        
        tokens = analisador.getTokens();

            //Exibe na tabela a lista de tokens
        InserirTabela(tokens);
    }//GEN-LAST:event_jBRunActionPerformed

    
    public void InserirTabela(List<Token> list) {
        Token token;
        javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) textoTabela.getModel();

        while (textoTabela.getRowCount() > 0) {
            model.removeRow(0);
        }

        for (int i = 0; i < list.size(); i++) {
            token = list.get(i);
            model.addRow(new Object[]{ mostra_token(token.getToken()),  token.getSimbolo(), token.getLinha()+"X"+token.getColuna()});
        }

    }
    
    public String mostra_token(String token){
        
              
            
         switch (token) {
            case ")":
                return "";
               
            case "(":
                 return "";
            case "[":
                 return "";
            case "]":
                 return "";
            case "}":
                 return "";
            case "{":
                 return "";
             case ";":
                 return "";
             case "'":
                 return "";
             case "''":
                 return "";
             case ".":
                 return "";
             case ",":
                 return "";
             case "=":
                 return "";
             case "==":
                 return "";             
             case "<":
                 return "";
             case ">":
                 return "";
             case "!=":
                 return "";                 
            default:
                 return token;
         }        
      
    }
    
    
    
    
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.JButton jBRun;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTCodigoFonte;
    private javax.swing.JTable textoTabela;
    // End of variables declaration//GEN-END:variables
}
